/*
 * TrackRegGuiCompetition.java
 *
 * Created on 05.06.2009, 22:17:23
 */

package trackreggui;

import java.util.Date;
import javax.swing.JOptionPane;
import org.jdesktop.application.Action;
import webreg.model.Competition;
import webreg.model.exceptions.EmptyDescriptionException;
import webreg.model.exceptions.EmptyPlaceException;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author STKNOLL
 */
public class ViewCompetitionDetails extends javax.swing.JDialog {

    /** Creates new form TrackRegGuiCompetition */
    public ViewCompetitionDetails(java.awt.Frame parent) {
        super(parent);
        initComponents();
        getRootPane().setDefaultButton(cancelButton);
        mToday = new Date( System.currentTimeMillis() );
        dateTextField.setText(mToday.toString()  );
        mCanceled = false;
    }

    private void closeCompetitionBox() {
        dispose();
    }

    @Action
    public void cancelCompetition() {
        mCanceled = true;
        closeCompetitionBox();
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        java.awt.GridBagConstraints gridBagConstraints;

        descriptionLabel = new javax.swing.JLabel();
        placeLabel = new javax.swing.JLabel();
        descriptionText = new javax.swing.JTextField();
        placeText = new javax.swing.JTextField();
        okButton = new javax.swing.JButton();
        cancelButton = new javax.swing.JButton();
        dateTextField = new javax.swing.JTextField();
        datumLabel = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(trackreggui.TrackRegGuiApp.class).getContext().getResourceMap(ViewCompetitionDetails.class);
        setTitle(resourceMap.getString("competition.title")); // NOI18N
        setModal(true);
        setName("competition"); // NOI18N
        setResizable(false);
        addComponentListener(new java.awt.event.ComponentAdapter() {
            public void componentShown(java.awt.event.ComponentEvent evt) {
                formComponentShown(evt);
            }
        });
        getContentPane().setLayout(new java.awt.GridBagLayout());

        descriptionLabel.setText(resourceMap.getString("descriptionLabel.text")); // NOI18N
        descriptionLabel.setName("descriptionLabel"); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(10, 10, 10, 10);
        getContentPane().add(descriptionLabel, gridBagConstraints);

        placeLabel.setText(resourceMap.getString("placeLabel.text")); // NOI18N
        placeLabel.setName("placeLabel"); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(10, 10, 10, 10);
        getContentPane().add(placeLabel, gridBagConstraints);

        descriptionText.setText(resourceMap.getString("descriptionText.text")); // NOI18N
        descriptionText.setName("descriptionText"); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.weightx = 0.8;
        gridBagConstraints.insets = new java.awt.Insets(10, 10, 10, 10);
        getContentPane().add(descriptionText, gridBagConstraints);

        placeText.setText(resourceMap.getString("placeText.text")); // NOI18N
        placeText.setName("placeText"); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.weightx = 0.8;
        gridBagConstraints.insets = new java.awt.Insets(10, 10, 10, 10);
        getContentPane().add(placeText, gridBagConstraints);

        javax.swing.ActionMap actionMap = org.jdesktop.application.Application.getInstance(trackreggui.TrackRegGuiApp.class).getContext().getActionMap(ViewCompetitionDetails.class, this);
        okButton.setAction(actionMap.get("createCompetition")); // NOI18N
        okButton.setText(resourceMap.getString("okButton.text")); // NOI18N
        okButton.setToolTipText(resourceMap.getString("okButton.toolTipText")); // NOI18N
        okButton.setMaximumSize(new java.awt.Dimension(87, 23));
        okButton.setMinimumSize(new java.awt.Dimension(87, 23));
        okButton.setName("okButton"); // NOI18N
        okButton.setPreferredSize(new java.awt.Dimension(87, 23));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 3;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        getContentPane().add(okButton, gridBagConstraints);

        cancelButton.setAction(actionMap.get("cancelCompetition")); // NOI18N
        cancelButton.setText(resourceMap.getString("cancelButton.text")); // NOI18N
        cancelButton.setToolTipText(resourceMap.getString("cancelButton.toolTipText")); // NOI18N
        cancelButton.setName("cancelButton"); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.gridy = 3;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        getContentPane().add(cancelButton, gridBagConstraints);

        dateTextField.setText(resourceMap.getString("dateTextField.text")); // NOI18N
        dateTextField.setName("dateTextField"); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.weightx = 0.8;
        gridBagConstraints.insets = new java.awt.Insets(10, 10, 10, 10);
        getContentPane().add(dateTextField, gridBagConstraints);

        datumLabel.setText(resourceMap.getString("datumLabel.text")); // NOI18N
        datumLabel.setName("datumLabel"); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(10, 10, 10, 10);
        getContentPane().add(datumLabel, gridBagConstraints);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void formComponentShown(java.awt.event.ComponentEvent evt) {//GEN-FIRST:event_formComponentShown
        mCanceled = false;
    }//GEN-LAST:event_formComponentShown

    @Action
    public void createCompetition() {
        try {
            Competition newCompetition = new Competition(descriptionText.getText(), placeText.getText(), new Date( System.currentTimeMillis() ) );
            ModelController.getInstance().setCompetition(newCompetition);
        } catch (EmptyDescriptionException ex) {
            org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(trackreggui.TrackRegGuiApp.class).getContext().getResourceMap(ViewCompetitionDetails.class);
            JOptionPane.showMessageDialog(null, resourceMap.getString("errorEmptyDescription.text"), resourceMap.getString("errorEmptyDescription.title"), JOptionPane.ERROR_MESSAGE);
            Logger.getLogger(ViewCompetitionDetails.class.getName()).log(Level.SEVERE, null, ex);
        } catch (EmptyPlaceException ex) {
            org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(trackreggui.TrackRegGuiApp.class).getContext().getResourceMap(ViewCompetitionDetails.class);
            JOptionPane.showMessageDialog(null, resourceMap.getString("errorEmptyPlace.text"), resourceMap.getString("errorEmptyPlace.title"), JOptionPane.ERROR_MESSAGE);
            Logger.getLogger(ViewCompetitionDetails.class.getName()).log(Level.SEVERE, null, ex);
        }
        mCanceled = false;
        closeCompetitionBox();
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton cancelButton;
    private javax.swing.JTextField dateTextField;
    private javax.swing.JLabel datumLabel;
    private javax.swing.JLabel descriptionLabel;
    private javax.swing.JTextField descriptionText;
    private javax.swing.JButton okButton;
    private javax.swing.JLabel placeLabel;
    private javax.swing.JTextField placeText;
    // End of variables declaration//GEN-END:variables
    private Date mToday;
    private boolean mCanceled;

    /** Return whether the dialog has been canceled
     *
     * @return True if canceled, else false
     */
    public boolean isCanceled() {
        return mCanceled;
    }
}
